const app = getApp();
import $service from '../../../service'
let doctor_id;
Page({
  data: {
    doctorList: [],
    titleList: [{
      name: '家庭医生',
      val: '1'
    },{
      name: '我的医生',
      val: '2'
    }],
    titleActive: '1',
    triggered: true,
    typeList: [{
      id: '',
      name: '全部'
    },{
      id: '3',
      name: '家庭医生服务'
    },{
      id: '4',
      name: '复诊服务'
    },{
      id: '1',
      name: '视频问诊服务'
    },{
      id: '2',
      name: '图文问诊服务'
    }],
    myInquiryIndex: 0,
    noFocus: false,
    groups: [
      { text: '不再关注', type: 'warn', value: 0 },
      { text: '仍然关注', value: 1 }
    ],
  },
  selectTitle(e) {
    let {val} = e.currentTarget.dataset
    this.setData({
      titleActive: val,
      doctorList: []
    })
    this.getDoctorList()
  },
  onLoad() {
    app.editTabBar1();
  },
  onShow() {
    this.getDoctorList()
    wx.hideHomeButton()
  },
  getDoctorList() {
    let params = {}
    let listType
    if (this.data.titleActive === '1') {
      listType = 'getFamilyDoctorList'
    } else {
      listType = 'getMyDoctorList'
      params = {
        inquiryType: this.data.typeList[this.data.myInquiryIndex].id
      }
    }
    app.http[listType](params).then(res => {
      this.setData({
        doctorList: res.data
      })
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  onRefresh() {
    if (this._freshing) return
    this._freshing = true
    setTimeout(() => {
      this.getDoctorList();
      this.setData({
        triggered: false,
      })
      this._freshing = false
    }, 1000)
  },

  // 关注
  async cncren(e) {
    let {val, id} = e.currentTarget.dataset
    doctor_id = id;
    app.http.getConsultationFee({doctorId: id}).then(res => {
      if (res.data) {
        if (res.data.inquiryType === '3') {
          $service.showToast('当次问诊仍未结束，请结束后再尝试')
          return
        }
      }
      if (val == 1) {
        this.setData({
          noFocus: true
        })
      } else {
        this.concernDoctor(1)
      }
    })
  },
  concernDoctor(val) {
    let params = {
      "doctorId": doctor_id,
      "isConcern": val
    }
    app.http.concernDoctor(params).then(res => {
      $service.showToast(res.msg)
      this.getDoctorList()
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  btnClick(e) {
      let {value} = e.detail
      if (value == 0) {
          this.concernDoctor(0)
      }
      this.setData({
          noFocus: false
      })
  },
  // 跳转到清单页
  goDetail(e) {
    let {id} = e.currentTarget.dataset
    if (id) {
      $service.openPage('./inventory/inventory?id=' + id)
    } else {
      $service.showToast('暂无清单记录')
    }
  },
  // 检查家庭医生是否超时
  exceeds48hours(id) {
    return app.http.exceeds48hours({doctorId: id}).then(res => {
      return res.data
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  // 跳转到医生主页
  getDoctorDetail(e) {
    let {id} = e.currentTarget.dataset
    $service.openPage(`/pages/user_side/index/index_doctorlist/index_doctor_details/index_doctor_details?doctorId=${id}`)
  },
  bindPickerChange() {

  },
  // 跳转到聊天页面
  goChat(e) {
    let {index} = e.currentTarget.dataset
    let {logId, doctorIm, inquiryPatientId, inquiryType, doctorId, trueName, userIm} = this.data.doctorList[index]
    let chatType = 1;
    app.http.getConsultationFee({doctorId: doctorId}).then(res => { // 1为仅查看聊天记录，2为仅查看聊天记录，聊天，3为查看聊天记录，聊天，测量数据、上传图片、处方验单
      if (res.data) { 
        if (res.data.inquiryType == 3 || res.data.inquiryType == 4) {
          chatType = 2
        } else {
          inquiryType = 2 
        }
      } else {
        inquiryType = 2 
      }
      $service.openPage(`/pages/chat/user_chat/user_chat?logId=${logId}&imId=${doctorIm}&inquiryPatientId=${inquiryPatientId}&log_type=${inquiryType}&doctorId=${doctorId}&doctorName=${trueName}&myIM=${userIm}&chatType=${chatType}`)
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  bindPickerChange(e) {
    let {value} = e.detail
    this.setData({
      myInquiryIndex: value
    })
    this.getDoctorList()
  }
})